using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using Microsoft.Practices.CompositeUI;

namespace SCSFContrib.CompositeUI.WPF
{
	/// <summary>
	/// Extends <see cref="WindowsApplication{TWorkItem, TShell}"/> to support WPF applications that use a <see cref="Window"/> as their shell.
	/// </summary>
	/// <remarks>
	/// <para>
	/// It is recommended that the <see cref="ApplicationShellApplication{TWorkItem, TShell}"/> type be preferred as a base class for WPF-based
	/// CAB applications. Doing so requires less code and provides a more natural starting point for the application.
	/// </para>
	/// </remarks>
	/// <typeparam name="TWorkItem">
	/// The type of the root application work item.
	/// </typeparam>
	/// <typeparam name="TShell">
	/// The type for the shell to use.
	/// </typeparam>
	/// <example>
	/// The following example shows how a simple subclass of <c>WindowShellApplication</c> can be created for a WPF CAB application. The root
	/// work item for the application will be of type <c>RootWorkItem</c>, whilst the WPF <see cref="Window"/> instance is of type <c>Window1</c>:
	/// <code>
	/// <![CDATA[
	/// public sealed class SimpleApplication : WindowShellApplication<RootWorkItem, Window1>
	/// {
	/// 	[STAThread]
	/// 	public static void Main()
	/// 	{
	/// 		App app = new App();
	/// 		app.InitializeComponent();
	/// 		//start up the CAB application
	/// 		new SimpleApplication().Run();
	/// 	}
	/// }
	/// ]]>
	/// </code>
	/// Note that this class defines the entry point for the application, which should be specified  when compiling the application. Notice how
	/// the WPF <c>Application</c> is explicitly created and initialized before the CAB application is started.
	/// </example>
	public abstract class WindowShellApplication<TWorkItem, TShell> : WindowsApplication<TWorkItem, TShell>
		where TWorkItem : WorkItem, new()
		where TShell : Window, new()
	{
		/// <summary>
		/// Invoked to start the CAB application.
		/// </summary>
		protected override void Start()
		{
			if (Application.Current == null)
			{
				throw new InvalidOperationException("No application has been created.");
			}

			Application.Current.Run(Shell);
		}
	}
}
